package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Activity;
import cn.wolfcode.mapper.ActivityMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.UtilContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private ActivityMapper activityMapper;


    @Override
    public void save(Activity activity) {
        activityMapper.insert(activity);
    }

    @Override
    public void delete(Long id) {
        activityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Activity activity) {
        activityMapper.updateByPrimaryKey(activity);
    }

    @Override
    public Activity get(Long id) {
        return activityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Activity> listAll() {
        return activityMapper.selectAll();
    }

    @Override
    public PageInfo<Activity> query(QueryObject qo) {
        Long eid = UtilContext.getCurrentUser().getId();
        //用用户id去中间表查询aid
        List<Long> aids = activityMapper.selectById(eid);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Activity> activitys = activityMapper.selectForList(qo); //里面不需要自己写limit
        //遍历每一个投票题
        for (Activity activity:activitys) {
            //遍历获得该用户已经投票的题号
            for (Long id: aids) {
                //判断题号和用户已经投的题号
                if (activity.getId().equals(id)){
                    //相同,就给题号设置已投票的状态
                    activity.setWhether(true);
                }
            }
        }
        return new PageInfo<Activity>(activitys);
    }

    @Override
    public void saveRelation(Long id,Long activity_id) {
        Long employeeId = UtilContext.getCurrentUser().getId();
        Date date = new Date();
        activityMapper.saveRelation(employeeId,id,date,activity_id);
    }


}
